const interfaces = require("../../utils/urlconfig")

Page({

  /**
   * 页面的初始数据
   */
  data: {
    baitiao: [{
      desc: '不分期',
      tip: '先用后付，0服务费',
      select: true
    }, {
      desc: '分三期',
      tip: '先用后付，0服务费',
      select: false
    }, {
      desc: '分六期',
      tip: '先用后付，0服务费',
      select: false
    }, {
      desc: '分12期',
      tip: '先用后付，0服务费',
      select: false
    }, {
      desc: '分24期',
      tip: '先用后付，0服务费',
      select: false
    }],
    partData: [{
      count: 1,
      id: 'ca1',
      loopImgurl: ['/images/logos/Angular.png', '/images/logos/Flutter.png'],
      prices: '10.00',
      title: 'Angular零基础学习'
    }, {
      count: 1,
      id: 'ca2',
      loopImgurl: ['/images/logos/CSS3.png', '/images/logos/VUE.png'],
      prices: '10.00',
      title: 'CSS3零基础学习'
    }, {
      count: 1,
      id: 'ca3',
      loopImgurl: ['/images/logos/Flutter.png', '/images/logos/CSS3.png'],
      prices: '10.00',
      title: 'Flutterr零基础学习'
    },  {
      count: 1,
      id: 'ca4',
      loopImgurl: ['/images/logos/HTML5.png', '/images/logos/CSS3.png'],
      prices: '10.00',
      title: 'HTML5零基础学习'
    }, 
    {
      count: 1,
      id: 'ca5',
      loopImgurl: ['/images/logos/Node.png', '/images/logos/CSS3.png'],
      prices: '10.00',
      title: 'Node零基础学习'
    }, 
  ],
    baitiaoSelectItem: {
      desc: "【白条支付】首单享受立减优惠"
    },
    hideBaitiao: true,
    hideSelectItem: true,
    badgeCount: 0
  },
  handleBaitiaoView() {
    // console.log("显示白条弹框");
    this.setData({
      hideBaitiao: false
    })
  },
  handleCountView() {
    // console.log("显示购买数量弹框");
    this.setData({
      hideSelectItem: false
    })
  },
  updateCount(e) {
    // console.log(e)
    var partData = this.data.partData;
    partData.count = e.detail.count;
    // console.log(partDate);
    this.setData({
      partData: partData
    })

  },

  handleUpdate(e) {
    // console.log(e);
    this.setData({
      baitiaoSelectItem: e.detail
    })
  },
// 处理购物车内商品个数显示，红色小点点
  setBadge(cartArray){
    this.setData({
      badgeCount: cartArray.length
    })
  },
  //添加到购物车
  addToCart() {
    // console.log(this.data.partData);
    const self = this;
    wx.getStorage({
      key: 'cartInfo',
      success(res) {
        // console.log(res);
        // 现将本地存储的数据存起来
        const cartArray = res.data;

        //拿到当前商品数据
        let partData = self.data.partData;

        //判断数组是否存在该商品
        let isExist = false;


        //已有商品，total累加
        cartArray.forEach(cart=>{
          if(cart.id == partData.id){
            isExist = true;
            cart.total += self.data.partData.count;

            wx.setStorage({
              data:cartArray,
              key:'cartInfo'
            })
          }
        })
        //如果不在数组中，将商品直接push进去
        if(!isExist){
          partData.total = self.data.partData.count;
          cartArray.push(partData);
          //本地存储
          wx.setStorage(({
            data: cartArray,
            key: 'cartInfo'
          }))
        }
        // 更新badge
        self.setBadge(cartArray);

      },
      fail() {
        // console.log("fail");
        let partData = self.data.partData;
        partData.total = self.data.partData.count;

        //存储数组
        let cartArray = [];
        cartArray.push(partData);

        //本地存储
        wx.setStorage({
          data: cartArray,
          key: 'cartInfo',
        })

          // 更新badge
          self.setBadge(cartArray);
      }
    })

    //加入购物车提醒
    wx.showToast({
      title: '成功加入购物车',
      icon:"success",
      duration:3000
    })
  },

  //跳转购物车页面实现
  handleCartView(){
    wx.switchTab({
      url: '/pages/cart/index',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // console.log(options.id);
    const id = options.id;
    const part = this.data.partData;
    const self = this;
    // console.log(self.data);
    wx.showLoading({
      title: '等多阵啦~~',
    })

    wx.request({
      url: interfaces.productionsDetail,
      success(res) {
        console.log(res.data);
        let result = null;
        res.data.forEach(data => {
          if (data.partData.id == id) {
            result = data;
            console.log(result.partData.id)
          }
        })
        self.setData({
          partData: result.partData,
          baitiao: result.baitiao
        })
        wx.hideLoading()
      },
      fail(aka) {
        let result = null;
        part.forEach(data => {
          if (data.id == id) {
            result = data;
            console.log(result)
          }
        })
        self.setData({
          partData: result,
          baitiao: result.baitiao
        })
        // console.log(self.data);
        wx.hideLoading()
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    const self = this;
    wx.getStorage({
      key: 'cartInfo',
      success(res){
        const cartArray = res.data;
        self.setBadge(cartArray);
      }
    })
  }


})